package com.webservice.impl;


import com.dao.Maintenance_orderDao;
import com.dao.UserDao;
import com.domain.*;
import com.tool.Sms;
import com.tool.Until;
import com.webservice.UserWebservice;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.ArrayList;
import com.ibm.icu.text.Collator;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Logger;

@WebService(targetNamespace = "http://39.98.59.6/mmms_test/ws/user")
public class UserWebserviceImpl implements UserWebservice {

    @Autowired
    private UserDao userDao;

    @Autowired
    private Until until;

    @Autowired
    private Maintenance_orderDao maintenance_orderDao;

    /**
     * 根据手机号查询用户
     * @param tel
     * @return
     */
    @WebResult(name = "User")
    @Override
    public User findUserByTel(@WebParam(name = "tel")String tel){
        List<User> userList = userDao.isUser(tel);
        if (userList.size() >= 1){
            for (User user:userList){
                if (user.getAccount_state_id() == 2);
                user = until.findById(user.getId());
                return user;
            }
        }
        return null;
    }


    /**
     * 根据单位查询人员
     * @param insiderUnit
     * @return
     */
    @WebResult(name = "userList")
    @Override
    public List<User> findAllUserByInsiderUnit(@WebParam(name = "insiderUnit")Integer insiderUnit){
        List<User> userList = userDao.findUserByInsiderUnit(insiderUnit);
        List<User> users = new ArrayList<>();
        if (userList!=null){
            for (User user:userList){
                user = until.findById(user.getId());
                users.add(user);
            }
        }
        Collections.sort(users, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                //  重要部分
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getName(), o2.getName());

            }
        });
        return users;
    }

    @Override
    public void updateUser(@WebParam(name = "user") User user) {
        if (userDao.isUser(user.getTel()) != null){
            for (User user1:userDao.isUser(user.getTel())){
                if (user1.getAccount_state_id() == 2){
                    userDao.updateUser(user);
                }
            }
        }
    }

    /**
     * 新增方法
     * 新增单位信息
     * @param insiderUnit_name 单位名称
     * @return
     */
    @WebResult(name = "int")
    @Override
    public int addInsiderUnit(@WebParam(name = "insiderUnit_name")String insiderUnit_name){
        if (insiderUnit_name!=null && !insiderUnit_name.equals("")){
            userDao.addInsiderUnit(insiderUnit_name);
            return 0;
        }
        else {
            return -1;
        }
    }

    /**
     * 新增方法
     * 修改单位信息
     * @param insiderUnit_id 单位id
     * @param insiderUnit_name 单位名称
     * @return
     */
    @WebResult(name = "int")
    @Override
    public int updateInsiderUnit(@WebParam(name = "insiderUnit_id")Integer insiderUnit_id,
                                 @WebParam(name = "insiderUnit_name")String insiderUnit_name){
        userDao.updateInsiderUnit(insiderUnit_id, insiderUnit_name);
        return 0;
    }

    /**
     * 根据单位查询所有人员
     * @param insiderUnit_id
     * @return
     */
    @WebResult(name = "userList")
    @Override
    public List<User> findUserByInsiderUnit(@WebParam(name = "insiderUnit_id")Integer insiderUnit_id){
            List<User> userList = userDao.findUserByInsiderUnit(insiderUnit_id);
            List<User> users = new ArrayList<>();
            if (userList!=null){
                for (User user:userList){
                    user = until.findById(user.getId());
                    users.add(user);
                }
            }
        Collections.sort(userList, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                //  重要部分
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getName(), o2.getName());

            }
        });
        return users;
    }

    /**
     * 删除单位信息
     * @param insiderUnit_id
     * @return
     */
    @WebResult(name = "int")
    @Override
    public int delInsiderUnit(@WebParam(name = "insiderUnit_id")Integer insiderUnit_id){
        List<User> userList = userDao.findUserByInsiderUnit(insiderUnit_id);
        if (userList!=null){
            for (User user:userList){
                List<Maintenance_order> orders = maintenance_orderDao.findAllOrderByUser(user.getId());
                for (Maintenance_order order:orders){
                     int i = until.delOrder(order.getId());
                     if (i != 0){
                         return -1;
                     }
                }
                until.delUser(user.getId());
            }
        }
        userDao.delInsiderUnit(insiderUnit_id);
        return 0;
    }

    /**
     * 新增方法
     * 查找所有单位
     * @return
     */
    @WebResult(name = "insiderUnitList")
    public List<InsiderUnit> findAllInsiderUnit(){
        return userDao.findAllInsiderUnit();
    }



    @WebResult(name = "int")
    @Override
    public int register(@WebParam(name = "user")User user) {
        if (user != null && user.getRole_id() != null){
            if (userDao.findUsedUserByTel(user.getTel()).size() != 0){
                return -4; // 账号已存在
            }
            if (user.getRole_id() == -1){
                if (user.getAddress()!= null &&
                        user.getType_id()!=null &&
                        (user.getType_id() == 1 || user.getType_id() == 2 || user.getType_id() == 3)){
                    userDao.register(user);
                    userDao.addUnit(user);
                    if (!StringUtils.isEmpty(user.getBusiness_license()) &&
                            !StringUtils.isEmpty(user.getDetail())){
                        userDao.addCompany(user);
                    }
                    userDao.addRole(user.getId(), user.getRole_id());
                    userDao.addMaintenanceType(user.getId(), user.getType_id());

                }
                else {
                    return -2; //账号填写信息有误
                }
            }
            else  if (user.getRole_id() == -2){
                if (user.getAddress()!=null && user.getType_id()!=null &&
                        (user.getType_id() == 1 || user.getType_id() == 2 || user.getType_id() == 3)){
                    userDao.register(user);
                    userDao.addUnit(user);
                    userDao.addRole(user.getId(), user.getRole_id());
                    userDao.addMaintenanceType(user.getId(), user.getType_id());
                }
                else {
                    return -2;//账号填写信息有误
                }
            }
            else if (user.getRole_id() == 2){
                if (user.getInsiderUnit_id() != null){
                    userDao.register(user);
                }
                else {
                    return  -3;//账号填写信息有误
                }

                if (!StringUtils.isEmpty(user.getRank_name())){
                    userDao.addRank(user.getId(), user.getRank_name());
                }
                userDao.addRole(user.getId(), user.getRole_id());
                if (user.getInsiderUnit_id() != null){
                    userDao.addUserInsiderUnit(user.getId(), user.getInsiderUnit_id());
                }
                else {
                    return -3;//账号填写信息有误
                }
            }
            else if (user.getRole_id() == 3){
                if(user.getDepartment_id()!=null && (user.getDepartment_id() == 1 || user.getDepartment_id() == 2)){
                    userDao.register(user);
                    userDao.addDepartment(user.getId(), user.getDepartment_id());
                    userDao.addRank(user.getId(), user.getRank_name());
                    userDao.addRole(user.getId(), user.getRole_id());

                }
                else {
                    return -2;//账号填写信息有误
                }
                if (user.getInsiderUnit_id() != null){
                    userDao.addUserInsiderUnit(user.getId(), user.getInsiderUnit_id());
                }
                else {
                    return -3;//账号填写信息有误
                }

            }

            else if (user.getRole_id() == 4){

//                List<User> users = userDao.findReviewerByRank(user.getRank_id());
//                if (users!=null){
//                    return -4;//注册的审批员等级已被注册
//                }
                userDao.register(user);
                userDao.addRank(user.getId(), user.getRank_name());
                userDao.addRole(user.getId(), user.getRole_id());
            }
            else if (user.getRole_id() == -3){
                userDao.register(user);
                userDao.addRole(user.getId(), user.getRole_id());
            }
            else {
                return -3;
            }
        }
        else {
            return -1;
        };
        List<User> approvers = userDao.findApproverByDepartment(1);
        List<User> approvers1 = userDao.findApproverByDepartment(2);
        String approvers_tel = "";
        String sp1 = "";
        if (approvers1!=null){
            approvers.addAll(approvers1);
        }
        if (approvers!=null){
            for (User approver:approvers){
                if (approver.getTel().length() == 11){
                    approvers_tel = approvers_tel + sp1;
                    approvers_tel = approvers_tel+approver.getTel();
                    sp1=",";
                }
            }
            if(user.getRole_id() != 3){
                Sms.main(approvers_tel,"64999");//有新用户注册了，请尽快审核
            }
        }
        return 0;
    }



    @Override
    public void updateRole(@WebParam(name = "user_id")Integer user_id, @WebParam(name = "role_id")Integer role_id) {
        userDao.updateRole(user_id, role_id);

    }

    @Override
    public void updateAccount_state(@WebParam(name = "user_id")Integer user_id, @WebParam(name = "account_state_id")Integer account_state_id) {
        userDao.updateAccount_state(user_id, account_state_id);

    }

    @WebResult(name = "User")
    @Override
    public User findById(@WebParam(name = "user_id")Integer user_id) {
        User user = until.findById(user_id);
        return user;
    }

    @WebResult(name = "int")
    @Override
    public int updateDepartment(@WebParam(name = "user_id")Integer user_id, @WebParam(name = "department_id")Integer department_id) {
        userDao.updateDepartment(user_id, department_id);
        return 0;
    }

    @WebResult(name = "userList")
    @Override
    public List<User> findAllMaintenance_Person(@WebParam(name = "maintenance_type")Integer maintenance_type) {
        List<User> userList = userDao.findAllMaintenance_Person(maintenance_type);
        //开始自定义比较器
        Collections.sort(userList, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                //  重要部分
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getName(), o2.getName());

            }
        });
        return userList;
    }

    @WebResult(name = "userList")
    @Override
    public List<User> findUserByState(@WebParam(name = "account_state_id") Integer account_state_id) {
        List<User> userList = userDao.findUserByState(account_state_id);
        List<User> userList1 = new ArrayList<User>();
        for (User user:userList){
            if (user != null && user.getRole_id() != null){
                    user = until.findById(user.getId());
                    userList1.add(user);
            }
        }

        return userList1;
    }

    @Override
    @WebResult(name = "int")
    public int delUser(@WebParam(name = "user_id") Integer user_id) {
          int i = until.delUser(user_id);
          if (i == 0){
              return 0;
          }
          else {
              return -1;
          }
    }

    @Override
    public void resetPwd(@WebParam(name = "user_id") Integer user_id) {
        userDao.resetPwd(user_id);
    }

    @WebResult(name = "userList")
    @Override
    public List<User> findUserByDepartment_id(@WebParam(name = "department_id") Integer department_id) {
        List<User> userList = userDao.findUserByState(2);
        List<User> users = new ArrayList<User>();
        if (userList!=null){
            for (User user:userList){
                user = until.findById(user.getId());
                users.add(user);
            }
        }
        return users;
    }

    @Override
    public List<User> findReviewerByDepartment() {
        List<User> userList = userDao.findReviewerByDepartment();
        List<User> userList1 = new ArrayList<>();
        if (userList!=null){
            for (User user:userList){
                user = until.findById(user.getId());
                userList1.add(user);
            }
        }
        //开始自定义比较器
        if (userList1.size()>1){
            Collections.sort(userList1, new Comparator<User>() {
                @Override
                public int compare(User o1, User o2) {
                    //  重要部分
                    Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                    return com.compare(o1.getName(), o2.getName());

                }
            });
        }

        return userList1;
    }


    /**
     * 查询除自己外所有的内部人员
     * @param user_id 自己的id
     * @return
     */
    @WebResult(name = "userList")
    @Override
    public List<User> findAllInsider(@WebParam(name = "user_id")Integer user_id){
        List<User> users = userDao.findAllInsider(user_id);
        List<User> userList = new ArrayList<>();
        if (users!=null){
            for (User user:users){
                user = Until.findById(user.getId());
                userList.add(user);
            }
        }
        //开始自定义比较器
        if (userList.size()>1) {
            Collections.sort(userList, new Comparator<User>() {
                @Override
                public int compare(User o1, User o2) {
                    //  重要部分
                    Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                    return com.compare(o1.getName(), o2.getName());

                }
            });
        }
        return userList;

    }


    @WebResult(name = "int")
    @Override
    public int isUser(@WebParam(name = "tel") String tel) {
        List<User> userList = userDao.isUser(tel);
        if (userList!=null){
            int i = userList.size();
            return i;
        }
        else {
            return 0;
        }
    }

    @WebResult(name = "int")
    @Override
    public int getAccountReportNum(){
        List<User> userList = userDao.findUserByState(1);
        List<User> users = new ArrayList<>();
        for (User user:userList){
            if (user.getRole_id()!=3){
                user = until.findById(user.getId());
                users.add(user);
            }
        }
        return users.size();
    }
}
